package com.xixi.controller;


import com.xixi.Service.DepartmentService;
import com.xixi.Service.EmployeeService;
import com.xixi.demo.Department;
import com.xixi.demo.Employee;
import com.xixi.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
    private EmployeeService employeeService;
@Autowired
private DepartmentService departmentService;

@RequestMapping("/findAll")
public String findAll(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
    //实现查询所有员工
    List<Employee> list=employeeService.findAllEmployee();
    List<EmployeeVo> employeeVos=new ArrayList<>();
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
    for(Employee employee:list){
        System.out.println(employee);
        EmployeeVo employeeVo=new EmployeeVo();
        //id找到部门信息
        Department department=departmentService.findById(employee.getDept_id());
        //封装VO中
        employeeVo.setEmp_id(employee.getEmp_id());
        employeeVo.setDept_id(employee.getDept_id());
        employeeVo.setDept_name(department.getDept_name());
        employeeVo.setEmp_name(employee.getEmp_name());
        employeeVo.setTelephone(employee.getTelephone());
        employeeVo.setMajor_name(department.getMajor_name());
        employeeVo.setJob_name(employee.getJob_name());
        employeeVo.setJoin_date(simpleDateFormat.format(employee.getJoin_date()));
        employeeVos.add(employeeVo);
    }

httpServletRequest.getSession().setAttribute("listVO",employeeVos);

return "list";
/*return  null;*/
}

/*
* 添加*/
@RequestMapping("/addEmployee")
public String addEmployee(Employee employee){
    System.out.println(employee);
    employeeService.addEmployee(employee);
    return "redirect:/employee/findAll";

}




}
